<template>
  <a-modal v-model:visible="visible" :footer="false" :width="1000">
    <template #title>{{ currentRow.name }} 价格</template>
    <ma-form ref="formRef" v-model="form" :options="options" :columns="columns"  @submit="handlerSubmit"></ma-form>
  </a-modal>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { dict } from '@/api/system/dict'
import { Message } from '@arco-design/web-vue'
import carapi from '@/api/admin/car'

const visible = ref(false)
const currentRow = ref({ id: undefined, name: undefined })
const form = ref({
  car_id: 0,
  special_prices:[]
})
const formRef = ref()
const options = ref({
  showFormTitle: false,
  layout: 'vertical',
  onSubmitSuccess:() => {
    console.log('onSubmitSuccess')
  }
})
const car_id = ref(0)
const columns = ref([
  {
    title: '节假日价格', dataIndex: 'special_prices', formType: 'children-form', type: 'table', allowDeleteFirst: true,
    formList: [
      {title: '节日名称', dataIndex: 'name', rules: [{ required: true, message: '请输入节日名称' }]},
      { title: '日期时间', formType:'range', mode:'date', dataIndex: 'date', rules: [{ required: true, message: '请选择日期' }] },
      { title: '价格', dataIndex: 'price', rules: [{ required: true, message: '请输入价格' },{
        validator:(value, callback) => {
          if(!/^[1-9]\d*$/.test(value)){
            callback('价格只能为大于0的整数')
          }
        }
      }] }
    ]
  }
])


const open = (row) => {
  currentRow.value = row
  form.value.car_id = row.id
  form.value.special_prices = row.special_prices == null ? [] : [...row.special_prices]
  car_id.value = row.id
  // crudRef.value.requestData()
  visible.value = true
}

defineExpose({ open })

const handlerSubmit = async (data, done) => {
    let validate = await formRef.value.validateForm()
    console.log('validate', validate)
    if (!validate) {
      done(true)
      const params = {...currentRow.value, ...data}
      carapi.update(car_id.value, params).then(res => {
        currentRow.value.special_prices = params.special_prices
        Message.success('提交成功')
      }).catch(e => {
        Message.error('提交失败')
      }).finally(() => {
        done(false)
      })
    }
}
</script>

<style scoped>
.tips { color: red }
</style>